package com.example.myapplication4;


import android.content.Context;
import android.graphics.Canvas;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

public class TestButtonView extends androidx.appcompat.widget.AppCompatButton {

    private List<String> strings;
    private int realWidth, realHeiht;
    public TestButtonView(Context context) {
        super(context);
        strings = new ArrayList<>();
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        measure(widthMeasureSpec);
        measure(heightMeasureSpec);
        Log.e("onMeasure", "realWidth: " + realWidth + "realHeiht: " + realHeiht + "widthMeasureSpec" + widthMeasureSpec + "heightMeasureSpec" + heightMeasureSpec);
        setMeasuredDimension(realWidth, realHeiht);
    }

    private void measure(int measureValue) {
        int defalueWidthSize = 150;//默认宽度
        int defalueHeightSize = 200;//默认高度
        int mode = MeasureSpec.getMode(measureValue);
        int specValue = MeasureSpec.getSize(measureValue);
        Log.e("onMeasure", "mode: " + mode + "specValue: " + specValue);
        switch (mode) {
            //指定一个默认值
            case MeasureSpec.UNSPECIFIED:
                Log.e("onMeasure", "mode: " + mode + "UNSPECIFIED ");
                realWidth = defalueWidthSize;
                realHeiht = defalueHeightSize;
                break;
            //取测量值
            case MeasureSpec.EXACTLY:
                Log.e("onMeasure", "mode: " + mode + "EXACTLY ");
                realHeiht = specValue;
                realWidth = specValue;
                break;
            //取测量值和默认值中的最小值
            case MeasureSpec.AT_MOST:
                Log.e("onMeasure", "mode: " + mode + "AT_MOST ");
                realWidth = Math.min(defalueWidthSize, specValue);
                realHeiht = Math.min(defalueHeightSize, specValue);
                break;
            default:
                break;
        }
    }

    @Override
    public void draw(Canvas canvas) {
        for (int i = 0;i<100;i++){
            strings.add(i+"");
        }
        super.draw(canvas);
    }
}
